﻿'/*
' * FAHWatch7 Copyright Marvin Westmaas ( mtm )
' *
' * This program is free software; you can redistribute it and/or
' * modify it under the terms of the GNU General Public License
' * as published by the Free Software Foundation; version 2
' * of the License. See the included file GPLv2.TXT.
' * 
' * This program is distributed in the hope that it will be useful,
' * but WITHOUT ANY WARRANTY; without even the implied warranty of
' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' * GNU General Public License for more details.
' * 
' * You should have received a copy of the GNU General Public License
' * along with this program; if not, write to the Free Software
' * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
' */
'/*
Imports System.Text

Public Class clsStatistics
    Public dtStatistics As DateTime = #1/1/2000#
    Public WU_Completed As String = ""
    Public WU_EUE As String = ""
    Public ReadOnly Property SuccesRate As String
        Get
            ' Math.Round(100 - (iWU_EUE / (iWU_Total / 100)), 2).ToString & "%"
            If WU_Completed = "" Or WU_EUE = "" Then Return "100%"
            Return Math.Round(100 - ((CInt(WU_EUE) / (CInt(WU_Completed) / 10)) * 10), 2).ToString & "%"
        End Get
    End Property
    Public ComputationTime As String = ""
    Public TotalCredit As String = ""
    Public AveragePPD As String 'Avg ppd for each slot at current time 
    Public Class sClient
        Public Class sSlot
            Public ID As String = ""
            Public Hardware As String 'Match to gpu-index or info("cpu")
            Public WU_Completed As String = ""
            Public WU_EUE As String = ""
            Public ReadOnly Property SuccesRate As String
                Get
                    If WU_Completed = "" Or WU_EUE = "" Then Return "100%"
                    Return Math.Round(100 - ((CInt(WU_EUE) / (CInt(WU_Completed) / 10)) * 10), 2).ToString & "%"
                End Get
            End Property
            Public ComputationTime As String = ""
            Public TotalCredit As String = ""
            Public AveragePPD As String = ""
        End Class
        Public Slots As New Dictionary(Of String, sSlot)
        Public Name As String = ""
        Public WU_Completed As String = ""
        Public WU_EUE As String = ""
        Public ReadOnly Property SuccesRate As String
            Get
                If WU_Completed = "" Or WU_EUE = "" Then Return "100%"
                Return Math.Round(100 - ((CInt(WU_EUE) / (CInt(WU_Completed) / 10)) * 10), 2).ToString & "%"
            End Get
        End Property
        Public ComputationTime As String = ""
        Public TotalCredit As String = ""
        Public AveragePPD As String = ""
    End Class
    Public Clients As New Dictionary(Of String, sClient)
    Public ReadOnly Property Report As String
        Get
            Try
                Dim sb As New StringBuilder
                If Not IsNothing(Clients) Then
                    For Each Client In Clients.Values.ToList
                        sb.AppendLine("Client: " & Client.Name)
                        sb.AppendLine("Total wu's completed: " & Client.WU_Completed)
                        sb.AppendLine("Total wu's failed: " & Client.WU_EUE)
                        sb.AppendLine("Succes rate: " & Client.SuccesRate)
                        sb.AppendLine("Average PPD: " & Client.AveragePPD)
                        sb.AppendLine("Total credits: " & Client.TotalCredit)
                        sb.AppendLine("Computation time: " & Client.ComputationTime)
                        sb.AppendLine()
                        If Not IsNothing(Client.Slots) Then
                            For Each Slot In Client.Slots.Values.ToList
                                sb.AppendLine("Slot: " & Slot.ID & " - " & Slot.Hardware)
                                sb.AppendLine("Total wu's completed: " & Slot.WU_Completed)
                                sb.AppendLine("Total wu's failed: " & Slot.WU_EUE)
                                sb.AppendLine("Succes rate: " & Slot.SuccesRate)
                                sb.AppendLine("Average PPD: " & Slot.AveragePPD)
                                sb.AppendLine("Total credits: " & Slot.TotalCredit)
                                sb.AppendLine("Computation time: " & Slot.ComputationTime)
                                sb.AppendLine()
                            Next
                        End If
                    Next
                End If
                sb.AppendLine()
                sb.AppendLine("Totals:")
                sb.AppendLine("Total wu's completed: " & WU_Completed)
                sb.AppendLine("Total wu's failed: " & WU_EUE)
                sb.AppendLine("Succes rate: " & SuccesRate)
                sb.AppendLine("Average PPD: " & AveragePPD)
                sb.AppendLine("Total credits: " & TotalCredit)
                sb.AppendLine("Total computation time: " & ComputationTime)
                Return sb.ToString
            Catch ex As Exception
                WriteError(ex.Message, Err)
                Return ""
            End Try
        End Get
    End Property
    Shared mClientStatistics As clsStatistics
    Public ReadOnly Property CurrentStatistics As clsStatistics
        Get
            If mClientStatistics.dtStatistics = #1/1/2000# Then
                Dim dtNow As DateTime = DateTime.Now
                WriteLog("Generating statistics at " & dtNow.ToString & " :" & GenerateNewStatistics.ToString)
            End If
            Return mClientStatistics
        End Get
    End Property
    Shared Function GenerateNewStatistics() As Boolean
        Dim rVal As Boolean = False
        Dim mBackup As clsStatistics = mClientStatistics
        Try
            Dim c As clsStatistics = sqdata.Statistics()
            mClientStatistics = c
            rVal = True
        Catch ex As Exception
            WriteError(ex.Message, Err)
            rVal = False
        End Try
        If Not rVal Then
            mClientStatistics = mBackup
        End If
        Return rVal
    End Function
    Public ReadOnly Property Statistics(WorkUnits As List(Of clsWU)) As sClient ' used for history view with wu selection based on sql query
        Get
            Dim rV As New sClient
            Try
                Dim iWU_Total As Int32 = WorkUnits.Count
                Dim iWU_credit As Int32 = 0 'Wu's to count for avg credit
                Dim iWU_EUE As Int32 = 0
                Dim tsProcessing As New TimeSpan
                Dim iTotalCredit As Double = 0
                Dim iWU_TotalPPD As Double = 0
                For Each lWU As clsWU In WorkUnits
                    'iWU_total += 1 changed to not count unfinished work units
                    If lWU.PPD <> "" And lWU.PPD <> "0" Then
                        iWU_credit += 1
                        iTotalCredit += CDbl(lWU.Credit)
                        iWU_TotalPPD += CDbl(lWU.PPD)
                    End If
                    If Not lWU.CoreStatus.ToUpper.Contains("FINISHED_UNIT") Then
                        iWU_EUE += 1
                    End If
                    If lWU.Frames.Count = 1 Then
                        tsProcessing.Add(lWU.tsTPF)
                    ElseIf lWU.Frames.Count > 1 Then
                        tsProcessing = tsProcessing.Add(lWU.Frames(lWU.Frames.Count - 1).FrameDT - lWU.Frames(0).FrameDT)
                    End If
                Next
                rV.WU_Completed = iWU_Total.ToString
                rV.WU_EUE = iWU_EUE.ToString
                'rV.SuccesRate = Math.Round(100 - (iWU_EUE / (iWU_Total / 100)), 2).ToString & "%"
                rV.ComputationTime = String.Format("{0} days {1} hours {2} minutes and {3} seconds", tsProcessing.Days, tsProcessing.Hours, tsProcessing.Minutes, tsProcessing.Seconds)
                rV.TotalCredit = iTotalCredit.ToString
                rV.AveragePPD = Math.Round((iWU_TotalPPD / iWU_credit), 2).ToString
            Catch ex As Exception
                WriteError(ex.Message, Err)
            End Try
            Return rV
        End Get
    End Property
End Class
